﻿using Autofac;
using Autofac.Extras.DynamicProxy;
using Autofac.Extras.Quartz;
using System.Reflection;

namespace Kz.Telegram.Bot.Infrastructure.Autofac
{
    public static class AutofacExtensions
    {
        /// <summary>
        /// 注入服务
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="ns"></param>
        public static void RegisterService(this ContainerBuilder builder, string ns)
        {
            builder.RegisterAssemblyTypes(Assembly.Load(ns + ".Service"))
                .Where(c => c.Name.EndsWith("Service"))
                .InstancePerDependency()
                .AsImplementedInterfaces()
                .EnableInterfaceInterceptors();
        }

        /// <summary>
        /// 注入仓储
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="ns"></param>
        public static void RegisterRepository(this ContainerBuilder builder, string ns)
        {
            builder.RegisterAssemblyTypes(Assembly.Load(ns + ".Repository"))
                .Where(c => c.Name.EndsWith("Repository"))
                .InstancePerDependency()
                .AsImplementedInterfaces()
                .EnableInterfaceInterceptors();
        }

        /// <summary>
        /// 注入缓存
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="ns"></param>
        public static void RegisterCache(this ContainerBuilder builder, string ns)
        {
            builder.RegisterAssemblyTypes(Assembly.Load(ns + ".Cache"))
                .InstancePerDependency()
                .AsImplementedInterfaces()
                .EnableInterfaceInterceptors();
        }

        /// <summary>
        /// 注入调度器
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="ns"></param>
        public static void RegisterQuartz(this ContainerBuilder builder, string ns)
        {
            builder.RegisterModule(new QuartzAutofacFactoryModule());
            builder.RegisterModule(new QuartzAutofacJobsModule(Assembly.Load(ns + ".Job")));
        }
    }
}
